#include "Log.hpp"

using namespace myLog;


/* int main(){
    
    //std::unique_ptr<LogStrategy> flush = std::make_unique<DisplayStrategy>();
    std::unique_ptr<LogStrategy> flush = std::make_unique<FileStrategy>();
    
    flush->SyncLog("hekkasdsa");
    return 0;
} */


// 这样使用还是优点复杂了，其实文件名字和行号，是可以通过c/c++中的预处理宏来搞得

/* int main(){
    logger(LogLevel::DEBUG, "main.cpp", 17) << "hello " << "3.14 " << " 17";
    logger(LogLevel::DEBUG, "main.cpp", 18) << "hello " << "3.14 " << " 18";
    logger(LogLevel::DEBUG, "main.cpp", 19) << "hello " << "3.14 " << " 19";

    logger.FileEnable();
    logger(LogLevel::DEBUG, "main.cpp", 22) << "hello" << "3.14" << " 20";
    logger(LogLevel::DEBUG, "main.cpp", 23) << "hello" << "3.14" << " 21";
    logger(LogLevel::DEBUG, "main.cpp", 24) << "hello" << "3.14" << " 22";

    return 0;
} */



/* int main(){
    LOG(LogLevel::DEBUG) << "hello " << "3.14 " << " 34";
    LOG(LogLevel::DEBUG) << "hello " << "3.14 " << " 35";
    LOG(LogLevel::DEBUG) << "hello " << "3.14 " << " 36";

    ENABLE_FILE_LOG_STRATEGY();
    LOG(LogLevel::DEBUG) << "hello " << "3.14 " << " 39";
    LOG(LogLevel::DEBUG) << "hello " << "3.14 " << " 40";
    LOG(LogLevel::DEBUG) << "hello " << "3.14 " << " 41";

    
    return 0;
} */


//多线程的测试 -> 直接拿抢票系统来做
#include "Mutex.hpp"
#include <vector>
using namespace myMutex;
static int ticket = 1000;

Mutex _ticket_lock;

void* GetTicket(void* args){
    std::string name = static_cast<char*>(args);
    // 不断执行
    while(1){
        Lock_Guard guard(_ticket_lock);
        if(ticket > 0){
            usleep(1000);
            LOG(LogLevel::INFO) << name << " : 开始抢票, 当前票余量为" << ticket;
            //std::cout << name << " : 开始抢票, 当前票余量为"<< ticket << std::endl;
            ticket--;
        }
        else{
            LOG(LogLevel::INFO) << name << " : 票余量不足" << ticket;
           // cout << name << " : 票余量不足" << endl;
            break;
        }
    }
    // 先不关心返回值
    return nullptr;
}

#define DEFAULT_THREAD_NUM 4

int main(){
    //默认往显示器上打印
    

    std::vector<pthread_t> threads(DEFAULT_THREAD_NUM, 0);
    std::vector<std::string> names(DEFAULT_THREAD_NUM, "");
    
    for(int i = 1; i <= DEFAULT_THREAD_NUM; ++i){
        char* id = new char[64]{0};
        snprintf(id, 64, "thread_%d", i);
        names[i - 1] = id;
        delete[] id;

        // 创建线程
        int threadCreate_id = pthread_create(&threads[i], nullptr, GetTicket, (void*)names[i - 1].c_str());
        if(threadCreate_id == 0){
            LOG(LogLevel::INFO) << names[i - 1] << "线程创建成功";
            //std::cout << names[i - 1] << "线程创建成功" << std::endl;
        }
        else{
            LOG(LogLevel::INFO) << names[i - 1] << "线程创建失败";
            //std::cout << names[i - 1] << "线程创建失败" << std::endl;
        }      
        
        
    }


    for(auto& tid : threads){
        pthread_join(tid, nullptr);
    }

    return 0;
} 